﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;
using ConnectDatabase;
using System.Configuration;

namespace Crystal_Report_WithoutDB
{
    public partial class frmReportMain : DevExpress.XtraEditors.XtraForm
    {
        public frmReportMain()
        {
            InitializeComponent();
        }

        #region Attributes
        ConnectDB con = new ConnectDB(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ReportDocument rptDoc = new ReportDocument();
        DataSet1 ds = new DataSet1();
        DataTable dt = new DataTable();
        #endregion

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            try
            {
                dt.TableName = "SMS Report Year";
                dt = con.getReportYear("2012");
                ds.Tables[0].Reset();
                ds.Tables[0].Merge(dt);
                rptDoc.Load(@"..\..\Report\CR_Year.rpt");
                rptDoc.SetDataSource(ds);

                #region Cách truyền dữ liệu vào parameter
                #region khai báo biến
                ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
                #endregion

                crParameterDiscreteValue.Value = "Báo cáo thống kê tin nhắn SMS trong năm 2012";
                crParameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions["title"];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
                #endregion

                CRViewer_Thisyear.ReportSource = rptDoc;
                CRViewer_Thisyear.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// "Export pdf file button"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            try
            {
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                SaveFileDialog saveDialog = new SaveFileDialog();

                saveDialog.Filter = "pdf files (*.pdf)|*.pdf";//|excel files (*.xls)|*.xls|All files (*.*)|*.*";
                saveDialog.FilterIndex = 1;
                //saveDialog.RestoreDirectory = true;
                saveDialog.OverwritePrompt = true;
                saveDialog.ShowHelp = false;
                saveDialog.Title = "Save report in pdf file";

                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    CrDiskFileDestinationOptions.DiskFileName = saveDialog.FileName;
                    CrExportOptions = rptDoc.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }
                    rptDoc.Export();
                    MessageBox.Show("Save complete", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// "Export Excel file button"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                ExportOptions CrExportOptions;

                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                ExcelFormatOptions CrFormatTypeOptions = new ExcelFormatOptions();
                SaveFileDialog saveDialog = new SaveFileDialog();

                saveDialog.Filter = "excel files (*.xls)|*.xls";
                saveDialog.FilterIndex = 1;
                //saveDialog.RestoreDirectory = true;
                saveDialog.OverwritePrompt = true;
                saveDialog.ShowHelp = false;
                saveDialog.Title = "Save report in excel file";

                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    CrDiskFileDestinationOptions.DiskFileName = saveDialog.FileName;
                    CrExportOptions = rptDoc.ExportOptions;
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.Excel;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    rptDoc.Export();
                    MessageBox.Show("Save complete", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            string selectedTabPageName = xtraTabControl1.SelectedTabPage.Name;

            try
            {
                switch (selectedTabPageName)
                {
                    case "ThisyearTab":
                        {
                            dt = con.getReportYear(DateTime.Now.Year.ToString());
                            ds.Tables[0].Reset();
                            ds.Tables[0].Merge(dt);
                            rptDoc.Load(@"..\..\Report\CR_Year.rpt");
                            rptDoc.SetDataSource(ds);

                            #region Cách truyền dữ liệu vào parameter
                            #region khai báo biến
                            ParameterFieldDefinitions crParameterFieldDefinitions;
                            ParameterFieldDefinition crParameterFieldDefinition;
                            ParameterValues crParameterValues = new ParameterValues();
                            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
                            #endregion

                            crParameterDiscreteValue.Value = "Báo cáo thống kê tin nhắn SMS trong năm " + DateTime.Now.Year.ToString();
                            crParameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
                            crParameterFieldDefinition = crParameterFieldDefinitions["title"];
                            crParameterValues = crParameterFieldDefinition.CurrentValues;

                            crParameterValues.Clear();
                            crParameterValues.Add(crParameterDiscreteValue);
                            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
                            #endregion

                            CRViewer_Thisyear.ReportSource = rptDoc;
                            CRViewer_Thisyear.Refresh();
                        }
                        break;

                    case "LastyearTab":
                        {
                            dt = con.getReportYear((DateTime.Now.Year - 1).ToString());
                            ds.Tables[0].Reset();
                            ds.Tables[0].Merge(dt);
                            rptDoc.Load(@"..\..\Report\CR_Year.rpt");
                            rptDoc.SetDataSource(ds);

                            #region Cách truyền dữ liệu vào parameter
                            #region khai báo biến
                            ParameterFieldDefinitions crParameterFieldDefinitions;
                            ParameterFieldDefinition crParameterFieldDefinition;
                            ParameterValues crParameterValues = new ParameterValues();
                            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
                            #endregion

                            crParameterDiscreteValue.Value = "Báo cáo thống kê tin nhắn SMS trong năm " + (DateTime.Now.Year - 1).ToString();
                            crParameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
                            crParameterFieldDefinition = crParameterFieldDefinitions["title"];
                            crParameterValues = crParameterFieldDefinition.CurrentValues;

                            crParameterValues.Clear();
                            crParameterValues.Add(crParameterDiscreteValue);
                            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
                            #endregion

                            CRViewer_Lastyear.ReportSource = rptDoc;
                            CRViewer_Lastyear.Refresh();
                        }
                        break;

                    case "ThismonthTab":
                        {
                            dt = con.getReportMonth(DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString());
                            ds.Tables[0].Reset();
                            ds.Tables[0].Merge(dt);
                            rptDoc.Load(@"..\..\Report\CR_Month.rpt");
                            rptDoc.SetDataSource(ds);

                            #region Cách truyền dữ liệu vào parameter
                            #region khai báo biến
                            ParameterFieldDefinitions crParameterFieldDefinitions;
                            ParameterFieldDefinition crParameterFieldDefinition;
                            ParameterValues crParameterValues = new ParameterValues();
                            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
                            #endregion

                            crParameterDiscreteValue.Value = "Báo cáo thống kê tin nhắn SMS trong tháng " + DateTime.Now.Month.ToString() + " năm nay";
                            crParameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
                            crParameterFieldDefinition = crParameterFieldDefinitions["title"];
                            crParameterValues = crParameterFieldDefinition.CurrentValues;

                            crParameterValues.Clear();
                            crParameterValues.Add(crParameterDiscreteValue);
                            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
                            #endregion

                            CRViewer_Thismonth.ReportSource = rptDoc;
                            CRViewer_Thismonth.Refresh();
                        }
                        break;

                    case "LastmonthTab":
                        {
                            dt = con.getReportMonth(DateTime.Now.Year.ToString(), (DateTime.Now.Month - 1).ToString());
                            ds.Tables[0].Reset();
                            ds.Tables[0].Merge(dt);
                            rptDoc.Load(@"..\..\Report\CR_Month.rpt");
                            rptDoc.SetDataSource(ds);

                            #region Cách truyền dữ liệu vào parameter
                            #region khai báo biến
                            ParameterFieldDefinitions crParameterFieldDefinitions;
                            ParameterFieldDefinition crParameterFieldDefinition;
                            ParameterValues crParameterValues = new ParameterValues();
                            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
                            #endregion

                            crParameterDiscreteValue.Value = "Báo cáo thống kê tin nhắn SMS trong tháng " + (DateTime.Now.Month - 1).ToString() + " năm nay";
                            crParameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
                            crParameterFieldDefinition = crParameterFieldDefinitions["title"];
                            crParameterValues = crParameterFieldDefinition.CurrentValues;

                            crParameterValues.Clear();
                            crParameterValues.Add(crParameterDiscreteValue);
                            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
                            #endregion

                            CRViewer_Lastmonth.ReportSource = rptDoc;
                            CRViewer_Lastmonth.Refresh();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}